package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityVideoRotateBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;

public class VideoRotateActivity extends BaseAc<ActivityVideoRotateBinding> {

    public static String videoPath;
    private int flag = 0; // 0左 1右
    private boolean isImage;
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvVideoSbLeft.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mDataBinding.sbVideo.setProgress(Integer.parseInt(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), "ss")));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_rotate;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoRotateBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivTurnLeft.setOnClickListener(this);
        mDataBinding.ivTurnRight.setOnClickListener(this);
        mDataBinding.ivTurnJx.setOnClickListener(this);
        mDataBinding.ivGenerate.setOnClickListener(this);
        mDataBinding.tvDeriveVideo.setOnClickListener(this);
        mDataBinding.ivVideoPlay.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTurnLeft:
                flag = 0;
                clearSelection();
                mDataBinding.llTurnLeft.setBackgroundResource(R.drawable.kk001);
                break;
            case R.id.ivTurnRight:
                flag = 1;
                clearSelection();
                mDataBinding.llTurnRight.setBackgroundResource(R.drawable.kk001);
                break;
            case R.id.ivTurnJx:
                if (isImage){
                    isImage=false;
                    mDataBinding.llTurnJx.setBackground(null);
                }else {
                    mDataBinding.llTurnJx.setBackgroundResource(R.drawable.kk001);
                }
                break;
            case R.id.ivGenerate:
                if (flag==0){
                    if (isImage){
                        startRotate(90,true);
                    }else{
                        startRotate(90,false);
                    }
                }else if (flag==1){
                    if (isImage){
                        startRotate(270,true);
                    }else {
                        startRotate(270,false);
                    }
                }
                break;
            case R.id.tvDeriveVideo:
                saveVideo();
                break;
            case R.id.ivVideoPlay:
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.bofang1);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting1);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        //获取视频路径 时长
        if (TextUtils.isEmpty(videoPath)) return;
        videoLength = MediaUtil.getDuration(videoPath);
        //设置播放器
        setPlayer();
    }

    //旋转视频
    private void startRotate(int rotate, boolean isImage) {
        showDialog(getString(R.string.rotate_video_ing) + "0"+getString(R.string.unit_percent));
        EditorHolder.editor().videoRotate(videoPath, rotate, isImage, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                videoPath = outFilePath;
                mDataBinding.videoView.setVideoPath(videoPath);
                mDataBinding.videoView.seekTo(1);
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting1);
                mDataBinding.videoView.start();
                startTime();
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(R.string.rotate_video_def);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.rotate_video_ing) + progress + getString(R.string.unit_percent));
            }
        });
    }

    //临时保存视频
    private void saveVideo() {
        showDialog(getString(R.string.save_video_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {
                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //保存到相册
                        FileP2pUtil.copyPrivateVideoToPublic(mContext, videoPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startActivity(HomeActivity.class);
                            }
                        },500);
                    }
                });
            }
        }, 1000);
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        //设置seekbar
        mDataBinding.sbVideo.setMax(Integer.parseInt(TimeUtils.millis2String(videoLength, "ss")));
        mDataBinding.tvVideoSbLeft.setText("00:00");
        mDataBinding.tvVideoSbRight.setText(TimeUtil.getMmss(videoLength));
        mDataBinding.sbVideo.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mDataBinding.videoView.seekTo(seekBar.getProgress());
            }
        });
        //设置播放器
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvVideoSbLeft.setText("00:00");
                mDataBinding.sbVideo.setProgress(0);
                mDataBinding.ivVideoPlay.setImageResource(R.drawable.bofang1);
                mp.seekTo(1);
                stopTime();
            }
        });
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    private void clearSelection() {
        mDataBinding.llTurnLeft.setBackground(null);
        mDataBinding.llTurnRight.setBackground(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivVideoPlay.setImageResource(R.drawable.zanting1);
        mDataBinding.videoView.start();
        startTime();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTime();
    }

}